<template>
  <div class="system-container">
    <vab-query-form>
      <el-form :inline="true" :model="queryForm" @submit.native.prevent>
        <el-form-item class="pr8">
          <el-input
            v-model.trim="queryForm.APPLY_NUM"
            clearable
            placeholder="标本号"
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="queryForm.created_at"
            end-placeholder="结束日期"
            range-separator=" ~ "
            start-placeholder="录入开始日期"
            type="daterange"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
          <el-button icon="el-icon-plus" type="success" @click="handleEdit">
            新增记录
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @row-dblclick="handleDetail"
    >
      <el-table-column
        align="center"
        label="标本号"
        prop="apply_num"
        width="90"
      />
      <el-table-column
        align="center"
        label="ABO"
        prop="blood_group"
        width="90"
      />
      <el-table-column
        align="center"
        label="Rh(D)"
        prop="blood_rhd"
        width="90"
      />
      <el-table-column
        align="center"
        label="不规则抗体筛查"
        prop="blood_sop"
        width="120"
      />
      <el-table-column align="center" label="直抗" prop="dat" width="120" />
      <el-table-column
        align="center"
        label="Rh分型"
        prop="blood_rh_fx"
        width="100"
      />
      <el-table-column
        align="center"
        label="血小板抗筛"
        prop="blood_xxb_ks"
        width="100"
      />
      <el-table-column
        align="center"
        label="录入时间"
        prop="created_at"
        show-overflow-tooltip
        width="150"
      />
      <el-table-column align="center" label="操作" width="160">
        <template #default="scope">
          <el-link
            class="mr12"
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >
            修改
          </el-link>
          <el-link
            icon="el-icon-printer"
            size="mini"
            type="success"
            @click="print(scope.row)"
          >
            打印
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :pagesize="queryForm.pagesize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-dialog
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      :title="title"
      :visible.sync="dialogVisible"
      width="800px"
    >
      <report :blood-match="bloodMatch" />
    </el-dialog>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getAll as getList } from '@/api/bloodSpecimen'
  import Report from '../report'
  import Edit from './components/edit'
  import { removeEmptyField } from '@/utils'
  export default {
    name: 'BloodMatchSpecimen',
    components: {
      Report,
      Edit,
    },
    props: {
      activeName: {
        type: String,
        default() {
          return 'fourth'
        },
      },
    },
    data() {
      return {
        list: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        listLoading: false,
        selectRows: '',
        queryForm: {
          APPLY_NUM: '',
          PAT_NAME: '',
          created_at: '',
          page: 1,
          pagesize: 10,
        },
        dialogVisible: false,
        bloodMatch: null,
        title: '',
      }
    },
    watch: {
      'queryForm.MATCH_DATE': {
        handler(newVal) {
          this.queryForm.BEGIN_DATE = newVal[0]
          this.queryForm.END_DATE = newVal[1]
        },
        immediate: true,
        deep: true,
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleEdit(row) {
        if (row.apply_num) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      print(row) {
        console.log(row)
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(removeEmptyField(this.queryForm))
        this.list = data.data
        this.total = data.total
        this.listLoading = false
      },
      handleDetail(row) {
        console.log(row)
      },
      handleRefresh() {
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pagesize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .pr8 {
    padding-right: 8px;
  }
</style>
